﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BEGDC.PreContract.BLL;
using BEGDC.PreContract.BLL.Authentication;

namespace PreContractWeb.Report
{
    public partial class ByCatStatusNo : Page
    {
        private PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check user permission
            user = new PCIdentity(HttpContext.Current.User.Identity);
            if (!Page.IsPostBack)
            {
                BindContractType();
                tbxCurrentDate.Text = DateTime.Now.ToShortDateString();
                //attach client events
                btnExport.Attributes.Add("onClick",
                                         "RptExportToExcel('" + ReportHelper.ReportType.DelayReport +
                                         "');return false;");
                btnExport.Visible = false;
            }
            RunReport(user.EmployeeId);
        }

       
        private void RunReport(long employeeID)
        {
            string err = string.Empty;
            string reportHTML = string.Empty;
            string excelHTML = string.Empty;

            string rptTitle = string.Empty;
            int contractAdjust = int.Parse(ddlContractAdjust.SelectedValue);
            DateTime currentDate = new DateTime();
            if (!DateTime.TryParse(tbxCurrentDate.Text, out currentDate))
            {
                LabelReport_Quarterly.Text = "请输入正确的基准类型，如：（2010-12-31）";
                return;
            }
            try
            {
                reportHTML = ReportHelper.GetReportByCatStatusNo(employeeID,
                                                               ReportHelper.ReportType.ByCatStatusNo,
                                                               currentDate,
                                                               contractAdjust,
                                                               out err,
                                                               out excelHTML,
                                                               out rptTitle);

                if (err != string.Empty)
                {
                    LabelReport_Quarterly.Text = "";
                    divError.InnerHtml = err;
                    btnExport.Visible = false;
                }
                else
                {
                    LabelReport_Quarterly.Text = rptTitle;
                    ScrollReport1.ContentHtml = reportHTML;
                    ScrollReport1.InitControl();
                    //divReport.InnerHtml = reportHTML;
                    Session[ReportHelper.ReportType.DelayReport.ToString()] = excelHTML;
                    divError.InnerHtml = "";
                    btnExport.Visible = true;
                }
            }
            catch (Exception exception)
            {
               // throw;
                LabelReport_Quarterly.Text = "";
                btnExport.Visible = false;
                divError.InnerHtml = AlertMessage.GetReportDataError;
                //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.GetReportDataError  + "')</script>");
            }
        }

        protected void btnGetRpt_Click(object sender, EventArgs e)
        {
            RunReport(user.EmployeeId);
        }

        private void BindContractType()
        {
            BaseFunction.BindContractAdjustDropDownList(ddlContractAdjust, "", true);
            //ddlContractType.Items.Insert(0, new ListItem("All", "0"));
        }
    }
}
